//
//  MSFilterBar.m
//  JYG_Agent
//
//  Created by apple on 15/10/19.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSFilterBar.h"

@implementation MSFilterBar

-(void)initItems
{
    CGSize size=_scrollContainer.frame.size;
    int xpos=0;
    int count=(int)[self.dataArray count];
    
    //添加功能按钮
    for (int i=0;i< count; i++)
    {
        NSString* btnTitle=[self.dataArray objectAtIndex:i];
        
        GXButton* itemBtn=[GXButton buttonWithType:UIButtonTypeCustom];
        itemBtn.relayout=YES;
        itemBtn.tag=100+i;
        float width=size.width/count;
        itemBtn.frame=CGRectMake(xpos, 0, width, size.height);
        xpos+=itemBtn.frame.size.width;
        
        if (i<count-1) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(xpos, 10, 1, size.height-20)];
            line.image = [UIImage imageNamed:@"dot"];
            [_scrollContainer addSubview:line];
        }
        
        itemBtn.backgroundColor=[UIColor clearColor];
        
        [itemBtn setTitleColor:kBlueColor forState:UIControlStateSelected];
        [itemBtn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        
        itemBtn.titleLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:12];
        itemBtn.titleLabel.textAlignment=NSTextAlignmentCenter;
        
        UIImage* arrowImg=[UIImage imageNamed:@"arrowUp"];
        [itemBtn setImage:arrowImg forState:UIControlStateSelected];
        [itemBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        
        [itemBtn setTitle:btnTitle forState:UIControlStateNormal];
        [itemBtn setTitle:btnTitle forState:UIControlStateSelected];
        
        [self relayoutBtnWithTitle:btnTitle forBtn:itemBtn];

        itemBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
        
        [itemBtn addTarget:self action:@selector(TransferResponse:) forControlEvents:UIControlEventTouchUpInside];
        
        [items insertObject:itemBtn atIndex:i];
        [_scrollContainer addSubview: itemBtn];
        
    }
    [_scrollContainer setContentSize:CGSizeMake(xpos,size.height)];
}

- (void)resetBtnState
{
    for (int i=0;i< self.dataArray.count; i++)
    {
        GXButton *btn = (GXButton *)[_scrollContainer viewWithTag:100+i];
        [self setTitle:self.dataArray[i] forBtn:btn];
        btn.selected = NO;
    }
}

- (void)relayoutBtnWithTitle:(NSString *)btnTitle forBtn:(GXButton *)btn
{
    CGSize size=_scrollContainer.frame.size;
    int count=(int)[self.dataArray count];
    float btnWidth=size.width/count;
    UIImage* arrowImg=[UIImage imageNamed:@"arrowUp"];
    CGSize btnTitleSize = [HDUtil getSizeWithText:btnTitle fontSize:12];
    CGFloat maxBtnTitleWidth = btnWidth-arrowImg.size.width-3;
    CGFloat btnTitleWidth = btnTitleSize.width>maxBtnTitleWidth?maxBtnTitleWidth:btnTitleSize.width;
    btn.titleLabelFrame=CGRectMake((maxBtnTitleWidth-btnTitleWidth)/2.0, 0, btnTitleWidth, size.height);
    btn.imageViewFrame=CGRectMake((maxBtnTitleWidth-btnTitleWidth)/2.0+btnTitleWidth+3, (size.height-arrowImg.size.height)/2.0, arrowImg.size.width, arrowImg.size.height);
}

- (void)setTitle:(NSString *)title forBtn:(GXButton *)btn
{
    [self relayoutBtnWithTitle:title forBtn:btn];
    [btn setTitle:title forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
}

@end
